

package com.gs.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.boot.actuate.health.HttpCodeStatusMapper;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 响应数据
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@ApiModel(value = "响应")
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 成功标志：true表示成功，false表示失败
     */
    @ApiModelProperty(value = "成功标志：true表示成功，false表示失败")
    private boolean success = true;

    /**
     * 编码：200表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：0表示成功，其他值表示失败")
    private int code = 200;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg = "success";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;


    public R<T> success(T data) {
        this.setData(data);
        return this;
    }

    public static <T> R<T> ok() {
        return new R();
    }

    public static <T> R<T> ok(T data) {
        return new R().success(data);
    }

    public boolean success(){
        return code == 0 ? true : false;
    }

    public R<T> error() {
        success = false;
        this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.msg = "后台报错了, 找后端解决";
        return this;
    }

    // 国际化以后再说
    public R<T> error500(int code) {
        success = false;
        this.code = code;
        this.msg = "后台报错了, 找后端解决";
        return this;
    }

    public R<T> error500(int code, String msg) {
        success = false;
        this.code = code;
        this.msg = msg;
        return this;
    }

    public R<T> error500(String msg) {
        success = false;
        this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.msg = msg;
        return this;
    }

    public static <T> R<T> error(String data) {
        return new R().error500(data);
    }

    public static <T> R<T> error(int code) {
        return new R().error500(code);
    }

    public static <T> R<T> error(int code, String msg) {
        return new R().error500(code, msg);
    }

    public boolean getSuccess() {
        return success;
    }
    public void setSuccess(boolean success) {
        this.success = success;
    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
